/* 
 * 定义itfreer.arcgis.draw绘制模块
 */
(function(window, angular, undefined){
	/* 
	 * 定义ArcGIS的绘制模块
	 */
	var itfreerarcgisdrawModule = angular.module('itfreer.arcgis.draw', ['itfreer.arcgis.core']);

	/* 
	 * 定义ArcGIS的绘制模块
	 */
	itfreerarcgisdrawModule.factory('$itfreer_arcgis_draw', 
		function($rootScope, $itfreer_core, $itfreer_arcgis_core, $gisObject) {
			$itfreer_arcgis_draw = {
				/* 
				 * 绘制完成的事件
				 */
				onDrawEndEventFunction : null,

				/* 
				 * 初始化绘制工具
				 */
				init : function(){
					dojo.connect($gisObject.drawToolbars, "onDrawEnd", $itfreer_arcgis_draw.onDrawEnd);
				},

				/* 
				 * 绘制完成事件
				 */
				onDrawEnd : function(geometry){ 
					if($itfreer_arcgis_draw.onDrawEndEventFunction){
						$itfreer_arcgis_draw.onDrawEndEventFunction(geometry);
					}
				},

				/* 
				 * 获取点样式
				 */
				getPointSymbol : function(){ 
					// 点样式
					var pointSymbol = new esri.symbol.SimpleMarkerSymbol(
						esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10,
						new esri.symbol.SimpleLineSymbol(
								esri.symbol.SimpleLineSymbol.STYLE_SOLID,
								new dojo.Color([
									$gisObject.styleConfig.drawStyle.strokeColor.r,
									$gisObject.styleConfig.drawStyle.strokeColor.g,
									$gisObject.styleConfig.drawStyle.strokeColor.b]), 1),
						new dojo.Color([$gisObject.styleConfig.drawStyle.fillColor.r,
								$gisObject.styleConfig.drawStyle.fillColor.g,
								$gisObject.styleConfig.drawStyle.fillColor.b,
								$gisObject.styleConfig.drawStyle.fillOpacity]));
					return pointSymbol;
				},

				/* 
				 * 获取线样式
				 */
				getLineSymbol : function(){ 
					// 线样式
					var lineSymbol = new esri.symbol.SimpleLineSymbol(
						esri.symbol.SimpleLineSymbol.STYLE_SOLID,
						new dojo.Color([$gisObject.styleConfig.drawStyle.strokeColor.r,
								$gisObject.styleConfig.drawStyle.strokeColor.g,
								$gisObject.styleConfig.drawStyle.strokeColor.b,
								$gisObject.styleConfig.drawStyle.fillOpacity]), 2);
					return lineSymbol;
				},

				/* 
				 * 获取填充样式
				 */
				getFillSymbol : function(){ 
					// 面样式
					var fillSymbol = new esri.symbol.SimpleFillSymbol(
						esri.symbol.SimpleFillSymbol.STYLE_SOLID,
						this.lineSymbol, new dojo.Color([
								$gisObject.styleConfig.drawStyle.fillColor.r,
								$gisObject.styleConfig.drawStyle.fillColor.g,
								$gisObject.styleConfig.drawStyle.fillColor.b,
								$gisObject.styleConfig.drawStyle.fillOpacity]));
					return fillSymbol;
				},
				
				/* 
				 * 开始绘制点
				 */
				drawPoint : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['POINT']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制多点
				 */
				drawMultiPoint : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['MULTIPOINT']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制线段
				 */
				drawLine : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['LINE']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制线
				 */
				drawPolyline : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['POLYLINE']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制自由线
				 */
				drawFreehandPolyline : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['FREEHANDPOLYLINE']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制面
				 */
				drawPolygon : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['POLYGON']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制自由面
				 */
				drawFreehandPolygon : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['FREEHANDPOLYGON']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制箭头
				 */
				drawArrow : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['ARROW']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制三角形
				 */
				drawTriangle : function(){
					$itfreer_arcgis_core.mapPan(); 
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['TRIANGLE']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制圆形
				 */
				drawCircle : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['CIRCLE']);
						$gisObject.isDrawing=true;
					}
				},
				
				/* 
				 * 开始绘制椭圆
				 */
				drawEllipse : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['ELLIPSE']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 开始绘制Extent
				 */
				drawExtent : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)){
						$gisObject.map.setMapCursor("url("+getJsBasePath()+"/arcgis/images/cur/crosshair.cur),auto");
					}
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.activate(esri.toolbars.Draw['EXTENT']);
						$gisObject.isDrawing=true;
					}
				},

				/* 
				 * 结束绘制
				 */
				deDraw : function(){ 
					$itfreer_arcgis_core.mapPan();
					if(!$itfreer_core.isNullOrUndefined($gisObject.drawToolbars)){
						$gisObject.drawToolbars.deactivate();
						$gisObject.isDrawing=false;
					}
				},

				/* 
				 * 清除绘制结果
				 */
				clearGraphics : function(){
					if(!$itfreer_core.isNullOrUndefined($gisObject.map)
						&& !$itfreer_core.isNullOrUndefined($gisObject.map.graphics)){
						$gisObject.map.graphics.clear();
					}
				},
		    
				/* 
				 * 结束绘制并清除绘制结果
				 */
				deDrawAndClearGraphics : function(){ 
					$itfreer_arcgis_draw.deDraw();
					$itfreer_arcgis_draw.clearGraphics();
					if($gisObject.map
						&& $gisObject.map.infoWindow){
						$gisObject.map.infoWindow.hide();
					}
				}
			};
			for(f in $itfreer_arcgis_draw){
				$rootScope[f] = $itfreer_arcgis_draw[f];
			}
			return $itfreer_arcgis_draw;
		}
	);

	/* 
	 * 加载服务器
	 */
	itfreerarcgisdrawModule.run(function($rootScope, $itfreer_arcgis_draw) {
	  	
	});
}(window, angular));